<script type="text/javascript">
var paginationTotal = 1;// 记录总数
$(function() {
			initData(0); // 初始化分页<{$lang.data}>

			$("#edit").dialog({
						autoOpen : false,
						width : 350,
						height : 300,
						modal : true,
						show : "slide",
						zIndex : 100
						
					});
					
			$(".messageDiv").dialog({
						autoOpen : false,
						width : 400,
						height : 200,
						modal : true,
						show : "slide",
						zIndex : 200,
						title : "<{$lang.tip}><{$lang.msg}>",
						buttons : {
							"<{$lang.confirm}>" : function() {
								$(this).dialog('close');
							},
							"<{$lang.close}>" : function() {
								$(this).dialog('close');
							}
						}
					});

			$("#confrimDiv").dialog({
						autoOpen : false,
						width : 400,
						height : 200,
						modal : true,
						show : "slide",
						zIndex : 200,
						title : "<{$lang.tip}><{$lang.msg}>"
					});
			});

function loadData(page, pageSize) {
	var area = $('[name=area]').val();
	var st_id = $('[name=st_id]').val();
	var warehouse_id = $('[name=warehouse_id]').val();
	var sp_calc_type = $('[name=sp_calc_type]').val();
	
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-price/index",
		data : {
			'page' : page,
			'pageSize' : pageSize,
			'area' : area,
			'st_id' : st_id,
			'warehouse_id' : warehouse_id,
			'sp_calc_type' : sp_calc_type
		},
		success : function(json) {
			paginationTotal = json.count;
			var html = "";
			if (json.count == 0) {
				html = "<td colspan='8' class=''><{$lang.no_data}></td>";
			} else {
				$.each(json.list, function(key, val) {
					html += "<tr>";
					html += "<td>" + val.sp_weight + "</td>";
					html += "<td>" + val.sp_cost + "</td>";
					html += "<td>" + val.currency_code + "</td>";
					html += "<td>" + val.area + "</td>";
					html += "<td>" + val.st_code + "(" + val.st_id + ")" + "</td>";
					html += "<td>" + val.sp_calc_type + "</td>";
					html += "<td>" + (val.warehouse_name ? val.warehouse_name : 'null') + "</td>";
					html += "<td><a href=\"javascript:editPage(" + val.sp_id
							+ ");\"><{$lang.edit}></a>&nbsp;&nbsp;<a href=\"javascript:void(0)\" onclick=\"alertInfo('"+ val.sp_id + "')\"><{$lang.delete}></a></td>";
					html += "</tr>";
				})
			}
			$("#list").html(html);

		}
	});
}

function search() {
	initData(0);
}

function showEditPage(obj, titles, asc) {
	if (asc == 'add') {
		obj.dialog({
					title : titles,
					buttons : {
						'<{$lang.add}>' : function() {
							editSubmit()
						},
						'<{$lang.close}>' : function() {
							$(this).dialog('close');
						}
					}
				});
	} else {
		obj.dialog({
					title : titles,
					buttons : {
						'<{$lang.edit}>' : function() {
							editSubmit()
						},
						'<{$lang.close}>' : function() {
							$(this).dialog('close');
						}
					}
				});
	}
	obj.dialog("open");
}

function editPage(sp_id) {
	if (sp_id == 0) {
		$('#edit [name=sp_weight]').focus();
		$('#edit [name=sp_id]').val(0);
		$('#editForm').resetForm();
		showEditPage($('#edit'), '<{$lang.shipping}><{$lang.price}><{$lang.add}>', 'add');
		return;
	}
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-price/info", // 请求的处理url
		data : {
			'sp_id' : sp_id,
		},
		success : function(json) {
			if (!json.ask) {
				var html = '<p class="alertMsg"><img src="/images/not.gif"><{$lang.shipping}><{$lang.price}><{$lang.msg}><{$lang.get}><{$lang.fail}></p>';
				$('#message').html(html);
				$('.messageDiv').dialog("open");
				return false;
			}
			var row = json.list;
			$('#edit [name=sp_id]').val(row.sp_id);
			$('#edit [name=sp_weight]').val(row.sp_weight);
			$('#edit [name=sp_cost]').val(row.sp_cost);
			$('#edit [name=currency_code]').val(row.currency_code);
			$('#edit [name=area]').val(row.area);
			$('#edit [name=st_id]').val(row.st_id);
			$('#edit [name=sp_calc_type]').val(row.sp_calc_type);
			$('#edit [name=warehouse_id]').val(row.warehouse_id);
		
			showEditPage($('#edit'), '<{$lang.edit}><{$lang.shipping}><{$lang.price}>', 'edit');
		}
	});
	
	
}


function editSubmit() {
	var formData = $('#editForm').serialize();
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-price/edit", // 请求的处理url
		data : formData,
		success : function(json) {
			var html = "";
			if (json.ask) {
				var html = '<p class="alertMsg"><img src="/images/yes.gif">'
						+ json.list.message + '</p>';
				$('#message').html(html);
				
			} else {
				var error = json.list.error;
				$.each(error, function(key, item) {
							html += '<p class="alertMsg"><img src="/images/no.gif">'
									+ item + '</p>';
						});
				$('#message').html(html);
			}
			$('.messageDiv').dialog("open");
			var curPage = $(".pagination span> .current").html();
				initData(curPage);
		}
	});

}

function alertInfo(sp_id) {
	$("#confrimDiv").dialog({
				buttons : {
					"<{$lang.confirm}>" : function() {
						del(sp_id)
					},
					"<{$lang.close}>" : function() {
						$(this).dialog('close')
					}
				}
			});
	$('#confrimMsg').html("<{$lang.delete_sure}>?");
	$('#confrimDiv').dialog("open");
}
function del(sp_id) {
	$('#confrimDiv').dialog("close");
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-price/delete", // 请求的处理url
		data : {
			'sp_id' : sp_id
		},
		success : function(json) {
			var html = "";
			if (json.ask) {
				var html = '<p class="alertMsg"><img src="/images/yes.gif">'
						+ json.list.message + '</p>';
				$('#message').html(html);
			} else {
				var error = json.list.error;
				$.each(error, function(key, item) {
							html += '<p class="alertMsg"><img src="/images/no.gif">'
									+ item + '</p>';
						});
				$('#message').html(html);
			}
			$('.messageDiv').dialog("open");
			var curPage = $(".pagination span> .current").html();
			initData(curPage);
		}
	});
}

</script>
<style type="text/css">
.button{background:url(images/left-menu-li.gif) repeat-x left top #F1F6F9; border:1px solid #318DAC; font-weight:bold; padding:2px 5px; font-weight:lighter; color:#318DAC; vertical-align:middle; -moz-border-radius:3px; -webkit-border-radius:3px; margin:auto 5px}
</style>

<div id="iTitle">
	<div id="iTitleSub">
		<form id="searchForm" name="searchForm" >
		<div>
		<label>
		<{$lang.zone}><{$lang.label}>：
        <input  class="myInput" type="text" name="area">
		</label>&nbsp;
        <label>
		<{$lang.shipping_method}>：
        <select  name="st_id" class="ui-widget-content">
        <option value=""><{$lang.pls}></option>
        <{foreach item="shipType" from=$shipTypeList key='key'}>
        <option value="<{$shipType.st_id}>"><{$shipType.st_code}></option>
        <{/foreach}>
       </select>
		</label>&nbsp;
        <label>
		<{$lang.warehouse}>：
        <select  name="warehouse_id" class="ui-widget-content">
        <option value=""><{$lang.pls}></option>
        <{foreach item="warehouse" from=$warehouseList key='key'}>
        <option value="<{$warehouse.warehouse_id}>"><{$warehouse.warehouse_name}></option>
        <{/foreach}>
       </select>
       </label>&nbsp;
        <label>
       <{$lang.cost}><{$lang.type}>:
       <select  name="sp_calc_type" class="ui-widget-content">
        <option value=""><{$lang.pls}></option>
        <{foreach from=$calcTypeList item=ctl key=k}>
        <option value="<{$k}>"><{$k}>--<{$ctl}></option>
        <{/foreach}>
       </select>
		</label>&nbsp;
		<input class="myButton" type="button" value="<{$lang.search}>" onclick="search();">&nbsp;&nbsp;
		<input class="myButton" type="button" value="<{$lang.add}>"  id="addButton" onclick="editPage(0)">&nbsp;&nbsp;
        <input class="myButton" type="button" value="<{$lang.batch}><{$lang.add}>"  id="addBatchButton" onclick="javascript:window.location='/shipping/ship-price/upload/type/ship_price'">&nbsp;&nbsp;
		</div>
		</form>
	</div>
	<div id="tipmsg"></div>

</div>
<div class="listDiv ui-widget-content"></div>
<h3><{$lang.shipping}><{$lang.price}><{$lang.list}></h3>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab">	
    <thead>
    <tr>
    <th><{$lang.weight}></th>
    <th><{$lang.price}></th>
    <th><{$lang.currency}></th>
    <th><{$lang.zone}><{$lang.label}></th>
    <th><{$lang.shipping_method}>(ID)</th>
    <th><{$lang.cost}><{$lang.type}></th>
    <th><{$lang.warehouse}></th>
    <th><{$lang.operation}></th>
    </tr>
    </thead>
	<tbody id="list"></tbody>
</table>
<div class="pagination"></div>
<div style="display:none;" class="messageDiv">
    <div id="message"></div>
</div>

<div style="display:none;" id="confrimDiv">
    <div id="confrimMsg"></div>
</div>


<div style="display:none;" id="edit" >
    <{include file='shipping/shipprice/edit.tpl'}>
</div>
